<?php
/**
 * redis缓存类
 * User: cory
 * Date: 2017/3/17
 * Time: 16:26
 */
class CacheRedis implements CacheInterface{


    // 服务器连接句柄
    private $_linkHandle = null;
    private $cache_sign = '';//缓存标识，用来标志相应的语言缓存

    public function connect($config,$lang){
        $this->_linkHandle['master'] = new Redis();
        $this->cache_sign = $lang;
        return $this->_linkHandle['master']->pconnect($config['host'],$config['port']);
    }

    /**
     * 关闭连接
     * @return boolean
     */
    public function close(){
        return $this->getRedis()->close();;
    }

    /**
     * 得到 Redis 对象
     */
    public function getRedis(){
        return $this->_linkHandle['master'];
    }

    /**
     * 写缓存
     *
     * @param string $key 组存KEY
     * @param string $value 缓存值
     * @param int $expire 过期时间， 0:表示无过期时间
     */
    public function set($key, $value, $expire=0){
        // 永不超时
        if($expire == 0){
            $ret = $this->getRedis()->set($this->cache_sign.'_'.$key, $value);
        }else{
            $ret = $this->getRedis()->setex($this->cache_sign.'_'.$key, $expire, $value);
        }
        return $ret;
    }

    /**
     * 读缓存
     */
    public function get($key){
        return $this->getRedis()->get($this->cache_sign.'_'.$key);
    }

    /**
     * 删除缓存
     * @param string $key
     * @return int 删除的健的数量
     */
    public function remove($key){
        return $this->getRedis()->delete($key);
    }

}